package com.gxa.weixing.portal.controller;

import com.gxa.weixing.core.common.JsonResult;
import com.gxa.weixing.core.pojo.Address;
import com.gxa.weixing.portal.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author HHT
 * 2022/1/11
 */
@RestController
@RequestMapping("/address")
@Api(value = "AddressController",tags = "地址的接口")
public class AddressController {

    @Autowired
    AddressService addressService;

    @PostMapping("/add")
    @ApiOperation("添加方法")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "address",value = "地址对象",required = true,dataType = "Long")
    )
    public Map add(@RequestBody Address address){
        addressService.adds(address);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/update")
    @ApiOperation("修改方法")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "address",value = "地址接口",required = true,dataType = "Long")
    )
    public Map update(@RequestBody Address address){
        addressService.updates(address);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

    @GetMapping("/findAll")
    @ApiOperation("查询全部")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "userId",value = "用户id",required = true,dataType = "Long")
    )
    public Map findAll(Long userId){
        Address address=new Address();
        address.setUserId(userId);
        List<Address> select = addressService.select(address);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        jsonResult.setData(select);
        return jsonResult.getDataMap();
    }

    @GetMapping("/delete")
    @ApiOperation("删除方法")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "addressId",value = "地址id",required = true,dataType = "Long")
    )
    public Map delete(Long addressId){
        Integer integer = addressService.deleteById(addressId);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }
}
